package cn.syy.controller.manager;

import cn.syy.result.R;
import cn.syy.template.CosTemplate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;

@Tag(name = "Cos文件上传",description = "Cos文件上传控制器")
@RestController
@RequestMapping("/cos")
public class CosController {
    @Autowired
    private CosTemplate cosTemplate;
    @Operation( summary= "文件上传",description = "文件上传接口")
    @Parameter(name = "uploadFile",description = "文件对象",required = true)
    @PostMapping("/uploadFile/{floder}")
    public R<String> uploadFile(@PathVariable("floder")String floder, @RequestBody @Valid MultipartFile file){
        try {
            // 获取文件本身
            InputStream inputStream = file.getInputStream();
            // 获取文件名，带后缀的
            String filename = System.currentTimeMillis() + file.getOriginalFilename();
            String filePath = cosTemplate.uploadFile(inputStream, floder, filename);
            System.out.println(filePath);
            return R.success(filePath);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("图片上传失败");
        }
    }
}
